package com.study.samsung.Control;

import com.alibaba.fastjson.JSONObject;
import com.study.samsung.Entity.Address;
import com.study.samsung.Entity.Commodity;
import com.study.samsung.Entity.Succession;
import com.study.samsung.Entity.User;
import com.study.samsung.Service.AddressService;
import com.study.samsung.Service.GoodsService;
import com.study.samsung.Service.IndexSearchService;
import com.study.samsung.Service.OrderXqService;
import com.study.samsung.Utils.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

//地址管理
@WebServlet("/Shippingaddress")
public class ShippingAddressServlet extends BaseServlet {
    AddressService as = new AddressService();
    IndexSearchService iss = new IndexSearchService();
    GoodsService gds = new GoodsService();
    @Override
    public String excute(HttpServletRequest request, HttpServletResponse response) throws Exception {

        User users = (User) session.getAttribute("user");
        Integer uid = users.getUid();

        List<Address> addresses = as.servletAll(uid);

        request.setAttribute("address",addresses);

        List<Commodity> newgoods = iss.selectnew();
        List<Succession> selectxl = iss.selectxl();

        request.setAttribute("newgoods",newgoods);
        request.setAttribute("selectxl",selectxl);

        Object user = session.getAttribute("user");
        if (user!=null){
            User nowuser = (User) user;
            Integer uids = nowuser.getUid();

            session.setAttribute("cartcount",gds.cartcount(uids));
        }
        return "f:Shippingaddress";
    }

    public Integer Upisdefault(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");

        String a = request.getParameter("aid");
        int i = Integer.parseInt(a);
        Integer b = as.changeIsdefault(i);
        if (b>0){
            return 1;
        }else{
            return 0;
        }
    }

    public Integer delAddress(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");
        String val = request.getParameter("aid");
        System.out.println(val);
        Integer a = Integer.parseInt(val);

        Integer b = as.delAddress(a);
        return b;
    }

    public String addnewAddress(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");
        String addressname = request.getParameter("addressname");
        String sheng = request.getParameter("sheng");
        String shi = request.getParameter("shi");
        String qu = request.getParameter("qu");
        String aname = request.getParameter("aname");
        String aphone = request.getParameter("aphone");
        User users = (User) session.getAttribute("user");
        Integer uid = users.getUid();
        String s;
        Address address = new Address(null,addressname,sheng,shi,qu,aname,aphone,1,uid);
        Integer r = as.addAddress(address);
        if (r>0){
            s = "成功添加地址！";
        }else{
            s="地址添加失败，请重新添加";
        }
        return s;
    }


    public String selectOne(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");

        String val = request.getParameter("aid");
        System.out.println(val);
        Integer a = Integer.parseInt(val);
        Address address = as.selectOne(a);
        String json = JSONObject.toJSONString(address);
        System.out.println(json);
        return json;
    }

    public String bianjiAddress(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");
        String jaid = request.getParameter("jaid");
        String addressname = request.getParameter("addressname");
        String sheng = request.getParameter("sheng");
        String shi = request.getParameter("shi");
        String qu = request.getParameter("qu");
        String aname = request.getParameter("aname");
        String aphone = request.getParameter("aphone");
        Integer aid = Integer.parseInt(jaid);
        String s;
        User users = (User) session.getAttribute("user");
        Integer uid = users.getUid();
        Address address = new Address(aid,addressname,sheng,shi,qu,aname,aphone,0,uid);
        Integer r = as.bianjiAddress(address);
        if (r>0){
            s = "地址编辑成功！";
        }else{
            s="地址编辑失败，请重新编辑";
        }
        return s;
    }
}
